import re

dbco = db.plugin_comments_comment

def error(): raise HTTP(404)

def SUL(*a,**b): return UL(*[u for u in a if u],**b)

#~ def plugin_component_id(tablename, record_id, plugin_name="comments"):
    #~ return "plugin_%s_%s_%s " % (plugin_name, tablename, record_id)

def script(cid=''):
    if cid:
        if not cid.startswith("#"):
            cid = "#"+cid
        trid = cid[len("#plugin_comments_"):]
    else:
        trid = cid = ''
        
    return SCRIPT("""
var action___trid__ = null;
var formhtml___trid__ = null;
function plugin_comments_init___trid__() {
  function delete_all_forms___trid__() { jQuery('__cid__ div.plugin_comments .reply').html(''); }
  function capture_form___trid__() {
     jQuery('__cid__ div.plugin_comments #close').click(function(){
       delete_all_forms___trid__();
     });
    jQuery('__cid__ div.plugin_comments :submit').click(function(){
      var form = jQuery(this).parent()
      var body = form.children('textarea[name="body"]').val();
      var url = action___trid__+'/'+form.parent().prev().attr('id');
      //~ alert('url:\\n'+url+'\\n\\n'+body);
       jQuery.post(url,
                  'body='+encodeURIComponent(body),
                   function(data,r){ if(data) { form.parent().next().prepend(data);
                                                form.children('textarea[name="body"]').val('')
                                                delete_all_forms___trid__();
                                                plugin_comments_init___trid__(); }
                   },'html');
      return false;
    });
  }
  jQuery('__cid__ div.plugin_comments #toggle').click(function(){
     jQuery(this).parent().next().next().slideToggle();
  });
  jQuery('__cid__ div.plugin_comments #reply').click(function(){
     delete_all_forms___trid__();
     jQuery(this).parent().next().next().slideDown();
     jQuery(this).parent().next().html(formhtml___trid__); capture_form___trid__(); 
  });
  jQuery('__cid__ div.plugin_comments #delete').click(function(){
    delete_all_forms___trid__();
    var parent = jQuery(this).parent()
    jQuery.post(action___trid__+'/delete/'+parent.attr('id'),null,function(data,r){parent.html('deleted');});
  });
}
jQuery(document).ready(function() {
  action___trid__ = jQuery('__cid__ div.plugin_comments form').attr('action');  
  formhtml___trid__ = jQuery('__cid__ div.plugin_comments form').parent().html();
  jQuery('__cid__ div.plugin_comments #toggle').parent().next().next().hide();
  jQuery('__cid__ div.plugin_comments .reply').html('');
  plugin_comments_init___trid__()}
);
""".replace('__cid__', cid).replace('__trid__', trid)
)

def index():
    tablename = request.args(0) or error()
    record_id = request.args(1) or error()
    cid = component_id('plugin_comments', tablename, record_id)
    parent_id = request.args(2) # must be None, 'delete', 'r0', 'r#'
    thread = {0:[]}
    def node(comment):
        if not comment.deleted:
            return LI(
                #~ DIV(I('"'+comment.body +'"'),BR(),
                # would be good to replace newlines with BR in comment.body
                DIV(PRE(comment.body), #BR(),
                    SPAN(
                        #~ T('posted by %(first_name)s %(last_name)s',comment.created_by),
                        '/ ',T('%(first_name)s ',comment.created_by),
                        ', ',T('%s',comment.created_on),'/',
                        _class="meta_info",
                     ),
                    # would like to wrap into span,
                    # but then dynamic interaction would break :/
                     #~ SPAN(
                    ' [',
                    A(T('toggle'),_id='toggle'),
                    '|' if auth.user_id else '',
                    A(T('reply'),_id='reply') if auth.user_id else '',
                    '|' if comment.created_by == auth.user_id else '',
                    A(T('delete'),_id='delete') if comment.created_by == auth.user_id else '',
                    ']',_id='r%s' % comment.id),

                    #~ _class="meta_info",
                    #~ ),
                DIV(_class='reply'),
                SUL(*[node(comment) for comment in thread.get(comment.id,[])]))
        elif comment.id in thread:
            return LI(
                DIV(T('DELETED'),' [',
                    A(T('toggle'),_id='toggle'),']'),
                DIV(_class='reply'),
                SUL(*[node(comment) for comment in thread.get(comment.id,[])]))
        else: 
            return None
    if parent_id == 'delete':
        if db(dbco.created_by==auth.user_id)(dbco.tablename==tablename)\
                (dbco.record_id==record_id)(dbco.id==request.args(3)[1:]).update(deleted=True):
            return 'deleted'
        else:
            return error()
    elif parent_id:
        if not request.vars.body or not auth.user_id:
            return error()
        dbco.parent_node.default = parent_id[1:]
        dbco.tablename.default = tablename
        dbco.record_id.default = record_id
        dbco.created_by.default = auth.user_id
        if len(re.compile('\s+').sub('',request.vars.body))<1:
            return ''
        item = dbco.insert(body=request.vars.body.strip())
        return node(item)                
    comments = db(dbco.tablename==tablename)(dbco.record_id==record_id).select(orderby=~dbco.created_on)
    for comment in comments:
        thread[comment.parent_node] = thread.get(comment.parent_node,[])+[comment]
    return DIV(script(cid),
               DIV(A(T('post'),_id='reply'),_id='r0') if auth.user_id \
                   else A(T('login to post'),_href=URL(r=request,c='default',f='user')),
               DIV(FORM(TEXTAREA(_name='body',_style='width:100%; height: 40px'),
                        INPUT(_type='submit',_value=T('post'),_style='float:right'), 
                        A(T('help'),_href='http://daringfireball.net/projects/markdown/',
                          _target='_blank',_style='float:right; padding-right: 10px'),
                        A(T('close'),_id='close',_style='float:right; padding-right: 10px'),
                        _method='post',_action=URL(r=request,args=[tablename,record_id])),_class='reply'),
               SUL(*[node(comment) for comment in thread[0]]),_class='plugin_comments')
